package panel.common;

import global.Const;

import javax.swing.*;
import java.awt.*;

public class LoadingBar extends JPanel {
    public static final int barWidth = 100;// 滑块宽度
    public static final int height = 50;  // 加载条高度
    public int speed = 5; // 滑块移动速度
    int x=0;

    public LoadingBar() {
        init();
    }

    private void init() {
        setBounds(0, Const.WINDOW_HEIGHT - height, Const.WINDOW_WIDTH, height);
        setOpaque(false);
    }

    public void moveBar() {
        x += speed;
        repaint();
    }

    public void reset() {
        x = 0;
        repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.setColor(Color.black);
        g2d.fillRect(x % getWidth() - getWidth(), 0, barWidth, height);
        g2d.fillRect(x % getWidth(), 0, barWidth, height);
    }
}
